package com.clearance.mine_clearance.untils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.entry.ButtenConfig;
import com.clearance.mine_clearance.entry.Config;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class RedisUntils {

    private static StringRedisTemplate stringRedisTemplate;


    static {
        stringRedisTemplate = (StringRedisTemplate) BeanUtils.getBean("stringRedisTemplate");
    }


    public static Set<String> getListKey(String prefix) {
        Set<String> keys = stringRedisTemplate.keys(prefix.concat("*"));
        return keys;
    }

    /**
     * 获取所有的key
     */
    public static List<String> getListKeyValue(String prefix) {
        Set<String> keys = stringRedisTemplate.keys(prefix.concat("*"));
        List<String> values = stringRedisTemplate.opsForValue().multiGet(keys);
        return values;
    }

    public static Config getConfig(){
        String configString = stringRedisTemplate.opsForValue().get(Constants.config);
        return JSON.parseObject(configString, Config.class);
    }

    public static List<ButtenConfig> getButtenConfig(){
        String configString = stringRedisTemplate.opsForValue().get(Constants.buttonConfig);
        List<ButtenConfig> buttenConfigList = JSONArray.parseArray(configString, ButtenConfig.class);
        return buttenConfigList;
    }




    public static void setValue(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }


    public static void setValueTime(String key, String value, Integer time, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value,time,timeUnit);
    }

    public static String getValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public static void rmove(String key) {
        stringRedisTemplate.delete(key);
    }
}
